<?php
if(!defined('DEDEINC')) exit('Request Error!');


/**
 * 网页元素标签
 * 
 * 支持的参数：
 * row		显示数量
 * type		位置标识，标识参见：/admin_back/inc/inc_web.class.php 内的配置
 * infolen	描述字数，可选
 * titlelen	标题字数，可选
 * 
 * 使用方式：
 * {dede:webad row=1 type='index_fast_nav' titlelen=30 infolen=50}
 * ....[field:字段名/]....
 * {/dede:webad}
 * 
 * 字段名列表：
 * id: 编号
 * link: 链接
 * title: 标题
 * info: 描述
 * img_url: 广告大图
 * litpic: 缩略图
 * add_time: 时间
 */

function lib_webad(&$ctag,&$refObj){
	 global $dsql;
	 
	 //属性处理
    $attlist="row|1,type|0,infolen|0,titlelen|0";
    FillAttsDefault($ctag->CAttribute->Items,$attlist);
    extract($ctag->CAttribute->Items, EXTR_SKIP);
    
    $innertext = $ctag->GetInnerText();
    $row = !empty($row) ? $row : 1;
    $where = " position='$type' ";
	
    //如果是子页面调用的，需要加typeid
    $ischannel = substr($type,0,7) == 'channel' ? true : false;
    if($ischannel){
    	 $typeid = $refObj->TypeLink->TypeInfos['id'];
    	 $where .= " AND arctype_id='$typeid' ";
    }
    $ctp = new DedeTagParse();
    $ctp->SetNameSpace('field', '[', ']');
    
    $html = '';
    $query = "SELECT web_id AS id, link, title, remark AS info, big_img AS img_url, small_img AS litpic, add_time FROM `#@__web` WHERE ".$where." ORDER BY `sort` ASC,add_time DESC LIMIT 0,".$row;	
    if($ischannel){
    	$isrow = $dsql->GetOne("SELECT web_id,link FROM `#@__web` WHERE $where LIMIT 1");
    	if(!$isrow){
    		$where = " position='".(str_replace('channel_', 'common_', $type))."'";
    		$query = "SELECT web_id AS id, link, title, remark AS info, big_img AS img_url, small_img AS litpic, add_time FROM `#@__web` WHERE ".$where." ORDER BY `sort` ASC,add_time DESC LIMIT 0,".$row;
    	}
    }
    $dsql->Execute('me', $query);
    $ctp->LoadSource($innertext);
	$ii = 0;
    while($rs = $dsql->GetArray('me')){
    	if($titlelen) $rs['title'] = cn_substr($rs['title'], $titlelen);
    	if($infolen) $rs['info'] = cn_substr($rs['info'], $infolen);
    	$rs['add_time'] = date('Y-m-d H:i:s', $rs['add_time']);
    	$rs['key'] = $ii;
		$rs['index'] = $ii+1;
		$ii++;
		foreach($ctp->CTags as $tagid=>$ctag)
        {
            if(isset($rs[$ctag->GetName()]))
            {
                $ctp->Assign($tagid,$rs[$ctag->GetName()]);
            }
        }
        $html .= $ctp->GetResult();
    }
    return $html;
}
?>